package com.iemacs.android.crt;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class SentencesAdapter extends ArrayAdapter<Sentence>{

	protected LayoutInflater mInflater; 
	protected Context mContext; 
	
	public SentencesAdapter(final Context c, int layout, final List<Sentence> list) {
		super(c, layout, list);
		this.mInflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext  = c;
		
	}
	
	public SentencesAdapter(final Context c, List<Sentence> list) {
		this(c, R.layout.sentence_item, list);
	}
	
	@Override
	public int getViewTypeCount() {
		return 1;
	}
	
	
	@Override
	public View getView(final int position, View convertView, final ViewGroup parent) {
		ViewHolder holder = null; 
		
		final Sentence s = getItem(position);
				
		if (convertView  == null) {
			holder = new ViewHolder();
			
			convertView = mInflater.inflate(R.layout.sentence_item,null);
			holder.tvChineseText = (TextView)convertView.findViewById(R.id.chinese_text);
			holder.tvRussianText = (TextView)convertView.findViewById(R.id.russian_text);
			holder.btnChinesePlay = (ImageView)convertView.findViewById(R.id.btnChinesePlay);
			holder.btnRussianPlay = (ImageView)convertView.findViewById(R.id.btnRussianPlay);
			
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder)convertView.getTag();
		}
		
		holder.tvChineseText.setText(s.getChineseText());
		holder.tvRussianText.setText(s.getRussianText());
		holder.btnChinesePlay.setTag("c"+s.getVoicePath());
		holder.btnChinesePlay.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)mContext).PlaySound("c"+s.getVoicePath());
			}
		});
		
		holder.btnRussianPlay.setTag("r"+s.getVoicePath());
		holder.btnRussianPlay.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				((MainActivity)mContext).PlaySound("r"+s.getVoicePath());
			}
		});		
		
		return convertView;
		
	}
	
	
	private static class ViewHolder {
		TextView tvChineseText;
		TextView tvRussianText;
		ImageView btnChinesePlay;
		ImageView btnRussianPlay;
	}

}
